package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CustomerDAO;
import com.dao.impl.CustomerDAOImpl;
import com.model.Customer;

/**
 * Servlet implementation class UpdateCustomerServlet
 */
public class UpdateCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 获取页面信息
		String customerNo = request.getParameter("customerNo");
		String customerName = request.getParameter("customerName");
		String telephone = request.getParameter("telephone");
		String address = request.getParameter("address");
		String id = request.getParameter("id");

		// 添加数据
		Customer customer = new Customer();
		customer.setCustomerNo(customerNo);
		customer.setCustomerName(customerName);
		customer.setTelephone(telephone);
		customer.setAddress(address);

		CustomerDAO cd = new CustomerDAOImpl();
		boolean b = cd.updateCustomer(customer,Integer.parseInt(id));

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"修改成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"修改失败!\"}";
		}
		response.getWriter().print(resultJson);
	}

}
